package com.shanwen.shopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shanwen.shopping.R;
import com.shanwen.shopping.entity.GoodsInfo;
import com.shanwen.shopping.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lex on 2017/10/9.
 */

public class NewGoodsAdapter extends BaseAdapter {

    private Context context;
    private List<GoodsInfo> list;
    private NumClickListener numClickListener;

    public NewGoodsAdapter(Context context,List<GoodsInfo> list){
        this.context = context;
        this.list = list;
    }

    public void setOnNumClickListener(NumClickListener numClickListener){
        this.numClickListener = numClickListener;
    }

    public void notifyData(List<GoodsInfo> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<GoodsInfo> getDataList(){
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view == null){
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_goods,null);
            holder.ivGoodsIcon = (ImageView) view.findViewById(R.id.ivGoodsIcon);
            holder.ivJia = (ImageView) view.findViewById(R.id.ivJia);
            holder.ivJian = (ImageView) view.findViewById(R.id.ivJian);
            holder.tvGoodsDesc = (TextView) view.findViewById(R.id.tvGoodsDesc);
            holder.tvGoodsPrice = (TextView) view.findViewById(R.id.tvGoodsPrice);
            holder.tvGoodNum = (TextView) view.findViewById(R.id.tvGoodNum);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        final GoodsInfo goodsInfo = list.get(position);
        Picasso.with(context).load(goodsInfo.getCoverUrl()).into(holder.ivGoodsIcon);
        if (goodsInfo.getNum()==0){
            holder.tvGoodNum.setText("");
            holder.ivJian.setVisibility(View.GONE);
        }else{
            holder.tvGoodNum.setText(goodsInfo.getNum()+"");
            holder.ivJian.setVisibility(View.VISIBLE);
        }
        holder.tvGoodsDesc.setText(goodsInfo.getTitle());
        holder.tvGoodsPrice.setText(Utils.formatPrice(goodsInfo.getSalePrice()));
        holder.ivJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numClickListener.jia(goodsInfo);
            }
        });
        holder.ivJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numClickListener.jian(goodsInfo);
            }
        });
        return view;
    }

    class ViewHolder{
        ImageView ivGoodsIcon;
        TextView tvGoodsDesc;
        TextView tvGoodsPrice;
        ImageView ivJia;
        ImageView ivJian;
        TextView tvGoodNum;
    }

    public interface NumClickListener{
        void jia(GoodsInfo goodsInfo);
        void jian(GoodsInfo goodsInfo);
    }
}
